<?php
class ControllerCommunityLottery extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('community/lottery');

        $this->load->model('community/lottery');

        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_community_lottery->editLottery($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/lottery', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['entry_prize'] = $this->language->get('entry_prize');
        $this->data['entry_item'] = $this->language->get('entry_item');
        $this->data['entry_probability'] = $this->language->get('entry_probability');

        $this->data['text_no_prize'] = $this->language->get('text_no_prize');
        $this->data['text_coupon'] = $this->language->get('text_coupon');
        $this->data['text_reward_point'] = $this->language->get('text_reward_point');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['item'])) {
            $this->data['error_item'] = $this->error['item'];
        } else {
            $this->data['error_item'] = array();
        }

        if (isset($this->error['prize'])) {
            $this->data['error_prize'] = $this->error['prize'];
        } else {
            $this->data['error_prize'] = array();
        }

        if (isset($this->error['probability'])) {
            $this->data['error_probability'] = $this->error['probability'];
        } else {
            $this->data['error_probability'] = array();
        }

        if (isset($this->error['probability_sum'])) {
            $this->data['error_probability_sum'] = $this->error['probability_sum'];
        } else {
            $this->data['error_probability_sum'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/lottery', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['cancel'] = $this->url->link('community/lottery', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['token'] = $this->session->data['token'];

        $lottery=$this->config->get('lottery');

        if (isset($this->request->post['lottery'])) {
            $this->data['lottery'] = $this->request->post['lottery'];
        } elseif(empty($lottery)) {
            $temp=array();

            for($i=0;$i<9;$i++){
                $temp[$i]['id']=$i+1;
                $temp[$i]['prize']='';

                if($i==8){
                    $temp[$i]['item']=$this->language->get('text_no_prize');
                }else{
                    $temp[$i]['item']='';
                }

                $temp[$i]['v']='';
            }

            $this->data['lottery'] = $temp;
        } else {
            $this->data['lottery'] = $this->model_community_lottery->getLottery();
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $this->template = 'community/lottery.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'community/lottery')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        $probability_sum=0;

        foreach($this->request->post['lottery'] as $key => $array){
            if(empty($array['item'])){
                $this->error['item'][$key] = $this->language->get('empty_item');
            }

            if($array['prize']==''){
                $this->error['prize'][$key] = $this->language->get('empty_prize');
            }

            if($array['v'] == ''){
                $this->error['probability'][$key] = $this->language->get('empty_probability');
            }

            $probability_sum+=$array['v'];
        }

        if($probability_sum!=10000){
            $this->error['probability_sum'] = $this->language->get('error_probability_sum');
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
?>